package com.ty.provider;

import com.ty.dto.OrderOk;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DelayProvider {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(OrderOk orderOk)
    {
        // 发送信息
        rabbitTemplate.convertAndSend( "delay-ex","", orderOk,
                message -> {
            Integer time = 0;
            switch (orderOk.getId().intValue()){
                case 1:
                    time = 60;
                    break;
                case 2:
                    time = 40;
                    break;
                case 3:
                    time = 20;
                    break;
                case 4:
                    time = 10;
                    break;
                case 5:
                    time = 1;
                    break; 
            }
            message.getMessageProperties().setDelay(time*1000);//消息在延迟交换机里停留的时间 单位毫秒
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT); //默认设置消息是持久化
            return message;
                });
        System.out.println("发送信息: " + orderOk);
    }
}
